use std::path::{Path, PathBuf};

/// Expands `~` to the user's home directory in a given path
pub fn expand_tilde(path: &PathBuf) -> PathBuf {
    println!("input path: {:?}", path);
    // let ss = path.components();
    // let ss1 = &ss.collect::<Vec<_>>();
    let ss: Vec<_> = path.components().collect(); // Collect all components into a Vec
    let ss1 = &ss; // Reference to all components
    let ss2 = &ss[1..]; // Reference to the components after skipping the first
    println!("Components: {:?}", ss1);
    println!("Components rest: {:?}", ss2);

    if let Some(str_path) = path.to_str() {
        println!("\nString path: {:?}", str_path);
        if str_path.starts_with("~") {
            println!("\n&str_path[1..]: {:?}", &str_path[1..]);
            println!("&str_path[2..]: {:?}", &str_path[2..]);
            if let Some(home_dir) = dirs::home_dir() {
                println!("\nHome dir: {:?}", home_dir);
                println!("home_dir.join(&str_path[1..]): {:?}", home_dir.join(&str_path[1..]));
                println!("home_dir.join(&str_path[2..]): {:?}", home_dir.join(&str_path[2..]));

                let mut my_comp = path.components();
                println!("\nmy_comp.next(): {:?}", my_comp.next());
                println!("my_comp.next(): {:?}", my_comp.next());

                let mut my_comp = path.components();
                my_comp.next();
                println!("\nmy_comp after next: {:?}", my_comp);
                println!("\nhome_dir.join(components): {:?}", home_dir.join(my_comp.as_path()));

                return home_dir.join(&str_path[2..]);
            }
        }
    }
    path.clone()
}
